package edu.sctu.zhuyingzhi.month201506.day20150628.theSecondTestV1;

import com.alibaba.fastjson.JSONObject;

import java.io.*;

/**
 * Created by user13 on 2015/6/28.
 */
public class JsonUtil {

    public MainData jsonToClass(String jsonFile) throws IOException {
        File file = new File(jsonFile);


        FileInputStream inputStream = new FileInputStream(file);
        String str = ClientGetPostTest.steamToString(inputStream);

//            MainData.getMainDate()  = JSONObject.parseObject(str,MainData.class);
        MainData mainData = JSONObject.parseObject(str, MainData.class);
        return mainData;


    }

    public void jsonClassToFile(MainData mainData,String filePath) {

        try {
            FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath));
            ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream);
            outputStream.writeObject(mainData);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public MainData readJson(String filePath) throws IOException, ClassNotFoundException {

        FileInputStream fileInputStream = new FileInputStream(new File(filePath));
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);


        MainData mainData = (MainData) objectInputStream.readObject();
        return mainData;
    }

    public void judgeGoods(MainData mainData) {
        int maxCount = 0;
        int minCount = 0;
        for (Goods good : mainData.list) {
            if (Float.parseFloat(good.goods_price) > 100) {
                maxCount++;
            }
            if (Float.parseFloat(good.goods_price) < 100) {
                minCount++;
            }
        }
        System.out.println("大于100为：" + maxCount);
        System.out.println("小于100为：" + minCount);


    }
}
